CXXFLAGS += $(INCLUDES) -fno-exceptions -std=c++0x

UNAME_P := $(shell uname -p)

ifdef ADDITIONALCXXFLAGS
	CXXFLAGS += $(ADDITIONALCXXFLAGS)
endif

ifeq ($(TARGET), Release)
	CXXFLAGS += -O3
endif

ifeq ($(TARGET), Debug)
	CXXFLAGS += -ggdb
endif

ifeq ($(CXX), clang++)
	ifneq ($(filter arm%, $(UNAME_P)),)
		CXXFLAGS += -mfloat-abi=hard
	endif
endif

MAKEDEPEND = $(CXX) -MM $(CXXFLAGS) $(ADDITIONALCXXFLAGS) -o $(OBJDIR)$(TARGET)/$*.d $(SOURCEDIR)$*.cpp

DEPLIBS:=$(addprefix -l, $(WNLIBS))
DEPLIBFILES:=$(addsuffix .a, $(addprefix $(WNLIBDIR)lib, $(WNLIBS)))
OBJECTS=$(addsuffix .o, $(addprefix $(OBJDIR)$(TARGET)/, $(basename  $(SOURCES))))
	
.PHONY: all

all: $(BINDIR)$(TARGET)/$(TOOL)

ifneq "$(MAKECMDGOALS)" "clean"
-include $(SOURCES:%.cpp=$(OBJDIR)$(TARGET)/%.d)
endif


$(BINDIR)$(TARGET)/$(TOOL):$(OBJECTS) $(DEPLIBFILES) $(TOOL_ADDITIONAL_LIB_FILES)
	test -d $(BINDIR) || mkdir -p $(BINDIR)
	test -d $(BINDIR)$(TARGET) || mkdir -p $(BINDIR)$(TARGET)
	$(CXX) -o $@ $(OBJECTS) $(ADDITIONALCXXFLAGS) -L $(WNLIBDIR) $(DEPLIBS) $(ADDITIONALLIBS) $(ADDITIONALLINKFLAGS) $(TOOL_ADDITIONAL_LIBS) $(LINKFLAGS) 

$(OBJDIR)$(TARGET)/%.o: $(OBJDIR)$(TARGET)/%.d
	@test -d $(OBJDIR) || mkdir -p $(OBJDIR) 
	@test -d $(OBJDIR)$(TARGET) || mkdir -p $(OBJDIR)$(TARGET)	
	$(CXX) $(CXXFLAGS) -c $(SOURCEDIR)$*.cpp -o $@ 

$(OBJDIR)$(TARGET)/%.d:$(SOURCEDIR)%.cpp
	@echo Make Depend $(SOURCEDIR)$*.cpp \>\> $(OBJDIR)$(TARGET)/$*.d
	@test -d $(OBJDIR) || mkdir -p $(OBJDIR) 
	@test -d $(OBJDIR)$(TARGET) || mkdir -p $(OBJDIR)$(TARGET)	
	@$(MAKEDEPEND)
	@mv $(OBJDIR)$(TARGET)/$*.d $(OBJDIR)$(TARGET)/$*.d.t
	@sed 's/$*.o/$(SEDDIR)$(TARGET)\/$*.o $(SEDDIR)$(TARGET)\/$*.d/' < $(OBJDIR)$(TARGET)/$*.d.t > $(OBJDIR)$(TARGET)/$*.d
	@rm -f $(SEDDIR)$(TARGET)/$*.d.t

clean:
	rm -rf $(OBJDIR)
